import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index.js'

//路由懒加载
const Home = () => import("@/views/Home/Home.vue")
const ListView = () => import("@/views/ListView/ListView.vue")
const Profile = () => import("@/views/Profile/Profile.vue")
const Yuncun = () => import("@/views/Yuncun/Yuncun.vue")
const BoKe = () => import("@/views/BoKe/BoKe.vue")
const Login = () => import("@/views/Login/Login.vue")


Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      title:'发现'
    }
  },
  {
    path:'/listview',
    component:ListView,
    meta:{
      title:'推荐歌单'
    }
  },
  {
    path:'/profile',
    component:Profile,
    meta:{
      title:'我的'
    },
	beforeEnter:(to,from,next)=>{
		// console.log(store.state.user.userinfo);
		if(store.state.user.isLogin === false ){
			next('/login')
		}else{
			next();
		}

	}
  },
  {
    path:'/yuncun',
    component:Yuncun,
    meta:{
      title:'云村'
    }
  },
  {
    path:'/boke',
    component:BoKe,
    meta:{
      title:'播客'
    }
  },
  {
    path:'/login',
    component:Login,
    meta:{
      title:'登录'
    }
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//导航守卫
router.beforeEach((to,from,next)=>{
  //from:从哪里来
  //to:到哪里去
  //next:执行
  document.title=to.meta.title;
  next()
})



export default router
